package com.hyt.it.ogt.opertaion.track.aspect;

import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.opertaion.track.annotations.AnswerTrackLogSave;
import com.hyt.it.ogt.opertaion.track.aspect.base.BaseServiceAspect;
import com.hyt.it.ogt.opertaion.track.model.AnswerLogVO;
import com.hyt.it.ogt.opertaion.track.queue.OperationTrackBlockQueue;
import com.hyt.it.ogt.opertaion.track.util.BeanUtils;
import com.hyt.it.ogt.opertaion.track.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 朱振宇
 * @since 2022/11/13
 */
@Slf4j
@Aspect
@Order
@Component
@ConditionalOnProperty(value = "answerLogAspect.enabled", havingValue = "true", matchIfMissing = true)
public class AnswerLogAspect extends BaseServiceAspect {

    // 配置织入点
    @Pointcut("execution(* *(..)) && @annotation(answerTrackLogSave)")
    public void statisticsPointCut(AnswerTrackLogSave answerTrackLogSave) {
    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "statisticsPointCut(answerTrackLogSave)", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, AnswerTrackLogSave answerTrackLogSave, Object jsonResult) {
        handleStatistics(joinPoint, answerTrackLogSave, jsonResult);
    }

    protected void handleStatistics(final JoinPoint joinPoint, AnswerTrackLogSave answerTrackLogSave, Object jsonResult) {
        try {
            boolean push = saveByJoinPoint(joinPoint, answerTrackLogSave) || saveByJsonResult(jsonResult, joinPoint, answerTrackLogSave);
            /*if (log.isDebugEnabled()) {
                log.debug("答题轨迹日志推送:{}", push);
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void execute(List<Map<String, Object>> fieldValMaps, Annotation annotation) {
        List<AnswerLogVO> answerLogVOList = BeanUtils.convertMapToBeanList(fieldValMaps, AnswerLogVO.class);
        String beginTime = DateUtils.nowMillSecond();
        AnswerTrackLogSave answerTrackLogSave = (AnswerTrackLogSave) annotation;
        answerLogVOList.forEach(vo -> {
            if (!StringUtils.isEmpty(answerTrackLogSave.eventTypeValue())) {
                vo.setEventType(Integer.valueOf(answerTrackLogSave.eventTypeValue()));
            }
            if (!StringUtils.isEmpty(answerTrackLogSave.operateTypeValue())) {
                vo.setOperateType(Integer.valueOf(answerTrackLogSave.operateTypeValue()));
            }
            if (!StringUtils.isEmpty(answerTrackLogSave.operateUserValue())) {
                vo.setOperateUser(answerTrackLogSave.operateUserValue());
            } else if (Objects.isNull(vo.getOperateUser())) {
                vo.setOperateUser(getUserId());
            }
            if (!StringUtils.isEmpty(answerTrackLogSave.beginTimeValue())) {
                vo.setBeginTime(answerTrackLogSave.beginTimeValue());
            } else if (Objects.isNull(vo.getBeginTime())) {
                vo.setBeginTime(beginTime);
            }
        });
        OperationTrackBlockQueue.producer(answerLogVOList);
        //String token = getToken();
        /*OperationTrackExecutorFactory.getThreadPoolExecutor().execute(()-> {
            if(token != null) {
                OperationTrackFeignInterceptor.setOperationTrackThreadLocal(HEADER_TOKEN, token);
            }
            ksOperationTrackClient.saveAnswerTrackLog(answerLogVOList);
        });*/
    }
}
